//
//  HYFSearchView.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/4.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "HYFSearchView.h"

@interface HYFSearchView()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldRightCon;

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@end


@implementation HYFSearchView


+ (instancetype)searchView{
    
    return [[NSBundle mainBundle]loadNibNamed:@"HYFSearchView" owner:nil options:nil].lastObject;
}


/**
 *  xib 子控件连线完成
 */
- (void)awakeFromNib{
    
    NSLog(@"%s---%@",__func__,self.textField);
    
    UIImageView *leftView = [[UIImageView alloc]init];
    
    leftView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    
    //设置显示模式
    leftView.contentMode = UIViewContentModeCenter;
    
    leftView.width = self.textField.height;
    leftView.height = self.textField.height;
    
    //设置 leftView 和 leftView 的显示模式
    self.textField.leftView = leftView;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    
    self.textField.delegate = self;
    
    self.textField.layer.cornerRadius = 8;
    self.textField.layer.masksToBounds = YES;
    
    self.textField.layer.borderWidth = 3;
    self.textField.layer.borderColor = [UIColor orangeColor].CGColor;
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
    self.textFieldRightCon.constant = self.cancelButton.width;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self layoutIfNeeded];
    }];
    
}


- (IBAction)buttonClick:(UIButton *)sender {
    
//    [self.textField resignFirstResponder];
    
    [self endEditing:YES];
    
    self.textFieldRightCon.constant = 0;
    
    [UIView animateWithDuration:0.25 animations:^{
        //如果有需要,就重新布局
        [self layoutIfNeeded];
    }];
}

/**
 *  从xib 里面加载出来,没有连线. 此方法归档解档(对文件存储)时也调用, xib 本身也是 xml 文件.
 *
 *  @param aDecoder <#aDecoder description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];

    if (self) {
        
          NSLog(@"%s---%@",__func__,self.textField);
        
    }
    return self;
}


@end
